/**
 * layout方法扩展
 * 
 * @param {Object}
 *            jq
 * @param {Object}
 *            region
 */
$.extend($.fn.layout.methods, {
	/**
	 * 面板是否存在和可见
	 * 
	 * @param {Object}
	 *            jq
	 * @param {Object}
	 *            params
	 */
	isVisible : function(jq, params) {
		var panels = $.data(jq[0], 'layout').panels;
		var pp = panels[params];
		if (!pp) {
			return false;
		}
		if (pp.length) {
			return pp.panel('panel').is(':visible');
		} else {
			return false;
		}
	},
	/**
	 * 隐藏除某个region，center除外。
	 * 
	 * @param {Object}
	 *            jq
	 * @param {Object}
	 *            params
	 */
	hidden : function(jq, params) {
		return jq.each(function() {
			var opts = $.data(this, 'layout').options;
			var panels = $.data(this, 'layout').panels;
			if (!opts.regionState) {
				opts.regionState = {};
			}
			var region = params;
			function hide(dom, region, doResize) {
				var first = region.substring(0, 1);
				var others = region.substring(1);
				var expand = 'expand' + first.toUpperCase() + others;
				if (panels[expand]) {
					if ($(dom).layout('isVisible', expand)) {
						opts.regionState[region] = 1;
						panels[expand].panel('close');
					} else if ($(dom).layout('isVisible', region)) {
						opts.regionState[region] = 0;
						panels[region].panel('close');
					}
				} else {
					panels[region].panel('close');
				}
				if (doResize) {
					$(dom).layout('resize');
				}
			}
			;
			if (region.toLowerCase() == 'all') {
				hide(this, 'east', false);
				hide(this, 'north', false);
				hide(this, 'west', false);
				hide(this, 'south', true);
			} else {
				hide(this, region, true);
			}
		});
	},
	/**
	 * 显示某个region，center除外。
	 * 
	 * @param {Object}
	 *            jq
	 * @param {Object}
	 *            params
	 */
	show : function(jq, params) {
		return jq.each(function() {
			var opts = $.data(this, 'layout').options;
			var panels = $.data(this, 'layout').panels;
			var region = params;

			function show(dom, region, doResize) {
				var first = region.substring(0, 1);
				var others = region.substring(1);
				var expand = 'expand' + first.toUpperCase() + others;
				if (panels[expand]) {
					if (!$(dom).layout('isVisible', expand)) {
						if (!$(dom).layout('isVisible', region)) {
							if (opts.regionState[region] == 1) {
								panels[expand].panel('open');
							} else {
								panels[region].panel('open');
							}
						}
					}
				} else {
					panels[region].panel('open');
				}
				if (doResize) {
					$(dom).layout('resize');
				}
			}
			;
			if (region.toLowerCase() == 'all') {
				show(this, 'east', false);
				show(this, 'north', false);
				show(this, 'west', false);
				show(this, 'south', true);
			} else {
				show(this, region, true);
			}
		});
	}
});
